package group.cyfc.studygo.controller.api;

import com.alibaba.fastjson.JSONObject;
import group.cyfc.studygo.controller.BaseController;
import group.cyfc.studygo.dto.JsonResult;
import group.cyfc.studygo.service.UserService;
import group.cyfc.studygo.vo.UserVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 用户服务功能控制器
 * @author Vonlin
 */
@Slf4j
@RequestMapping(value = {"/studygo"})
@Controller
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     *
     * 管理员登录
     * @param username
     * @param password
     * @return User
     */
    @ApiOperation(value = "管理员登录",notes = "管理员登录，返回一个User实体类型")
    @ApiImplicitParams({@ApiImplicitParam(name = "username",value = "管理员账号"),@ApiImplicitParam(name = "password",value = "管理员密码")})
    @GetMapping(value = "/managerLogin")
    public ResponseEntity<JsonResult> managerLogin(@Param("username")String username, @Param("password")String password){
        JsonResult result = userService.managerLogin(username,password);
        return new ResponseEntity<>(result,result.getCode());
    }


    /**
     * 用户登录
     * @param data
     * @return
     */
    @ApiOperation(value = "用户登录",notes = "若用户已注册，返回用户信息；否则返回用户openid")
    @ApiImplicitParam(name = "code",value = "微信访问code")
    @PostMapping(value = "/userLogin")
    public ResponseEntity<JsonResult> userLogin(@RequestBody JSONObject data){
        JsonResult userInfo = userService.userLogin(data);
        return new ResponseEntity<>(userInfo,userInfo.getCode());
    }

    /**
     *
     * 保存用户
     * @param userVo
     * @return
     */
    @ApiOperation(value = "保存用户",notes = "向数据库中保存一个用户")
    @ApiImplicitParam(name = "userVo",value = "一个Json,包含账号、用户名、年级、积分信息")
    @PostMapping(value = {"/saveUser"})
    public ResponseEntity<JsonResult> saveUser(@RequestBody UserVo userVo){
       JsonResult result = userService.saveUser(userVo);
       return new ResponseEntity(result, result.getCode());
    }

    /**
     *
     * 获取用户信息
     * @param username
     * @return
     */
    @ApiOperation(value = "获取用户信息",notes = "通过账号获取用户基本信息，返回一个User实体")
    @ApiImplicitParam(name = "username",value = "用户账号")
    @GetMapping(value = {"/getUserByUsername"})
    public ResponseEntity<JsonResult> getUserByUsername(@Param("username")String username){
        JsonResult result = userService.getUserByUsername(username);
       return new ResponseEntity<>(result,result.getCode());
    }

    /**
     *
     * 用户加分
     * @param userVo
     * @return
     */
    @ApiOperation(value = "用户加分",notes = "增加用户积分")
    @ApiImplicitParam(name = "userVo",value = "一个Json,包含用户的账号,用户所获得的积分")
    @PutMapping(value = {"/addScore"})
    public ResponseEntity<JsonResult> addScore(@RequestBody UserVo userVo){
        JsonResult result = userService.addScore(userVo);
        return new ResponseEntity<>(result,result.getCode());

    }

    /**
     *
     * 获取排行榜信息
     * @param username
     * @return
     */
    @ApiOperation(value = "排行榜信息",notes = "根据用户账号获取排行榜信息,返回排行榜信息与自己的排名信息，一个Json,里面是一个数组")
    @ApiImplicitParam(name = "username",value = "用户账号")
    @GetMapping(value = {"/getRanking"})
    public ResponseEntity<JsonResult> getRanking(@Param("username")String username){
        JsonResult result = userService.getRanking(username);
        return new ResponseEntity<>(result,result.getCode());
    }
}
